<?php
/**
 * Created by PhpStorm.
 * User: Digvijay
 * Date: 3/21/14
 * Time: 8:31 PM
 */

class Login extends CI_Controller {
    function index() {
        $this->load->view("login");
    }

    function verify() {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        try {
            $username = trim($this->input->post('username'));
            $password = trim($this->input->post('password'));
            if ($username != null && $username != "" && $password != null && $password != "") {
                $query = $this->doctrine->em->createQuery('SELECT u FROM User u WHERE u.username = :username and u.password = :password');
                $query->setParameter('username', $username);
                $query->setParameter('password', $password);
                $users = $query->getResult();
                if ($users != null) {
                    $this->session->set_userdata('logged_in', serialize($users[0]));
                    if ($this->session->userdata('logged_in')) {
                        echo '{"isValid": true}';
                    }
                } else {
                    echo '{"isValid": false,"message": null}';
                }
            }
        } catch (Exception $e) {
            echo '{"isValid": false,"message": "SERVER : Not able to Login. Please Contact Administrator"}';
        }
    }

    function logout(){
        $this->session->sess_destroy();
        $this->session->unset_userdata('logged_in');
//     delete_cookie("ci_session");
        redirect(base_url(),'refresh');
    }
} 